package com.cjkj.equipment.controller;

import com.cjkj.common.annotation.Log;
import com.cjkj.common.annotation.RepeatSubmit;
import com.cjkj.common.core.controller.BaseController;
import com.cjkj.common.core.domain.AjaxResult;
import com.cjkj.common.core.page.TableDataInfo;
import com.cjkj.common.enums.BusinessType;
import com.cjkj.common.utils.poi.ExcelUtil;
import com.cjkj.equipment.domain.TEquipmentInfo;
import com.cjkj.equipment.domain.dto.EquipmentDataReq;
import com.cjkj.equipment.domain.dto.EquipmentDistributionReq;
import com.cjkj.equipment.domain.vo.EquipmentDataVo;
import com.cjkj.equipment.domain.vo.EquipmentExportVo;
import com.cjkj.equipment.domain.vo.EquipmentImportVo;
import com.cjkj.equipment.service.ITEquipmentInfoService;
import com.cjkj.hospital.domain.THospitalEquipment;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备信息Controller
 * 
 * @author cjkj
 * @date 2024-08-26
 */
@RestController
@RequestMapping("/equipment/info")
public class TEquipmentInfoController extends BaseController
{
    @Resource
    private ITEquipmentInfoService tEquipmentInfoService;

    /**
     * 查询设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:info:list')")
    @GetMapping("/list")
    public TableDataInfo list(EquipmentDataReq req)
    {
        startPage();
        List<EquipmentDataVo> list = tEquipmentInfoService.getTEquipmentInfoList(req);
        return getDataTable(list);
    }

    /**
     * 导出设备信息列表
     */
    @PreAuthorize("@ss.hasPermi('equipment:info:export')")
    @Log(title = "设备信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EquipmentDataReq req)
    {
        List<EquipmentExportVo> list = tEquipmentInfoService.getTEquipmentExportList(req);
        ExcelUtil<EquipmentExportVo> util = new ExcelUtil<>(EquipmentExportVo.class);
        util.exportExcel(response, list, "设备信息数据");
    }

    /**
     * 获取设备信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:info:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(tEquipmentInfoService.selectTEquipmentInfoById(id));
    }

    /**
     * 新增设备信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:info:add')")
    @Log(title = "设备信息", businessType = BusinessType.INSERT)
    @RepeatSubmit(interval = 1000, message = "请求过于频繁！")
    @PostMapping
    public AjaxResult add(@RequestBody TEquipmentInfo tEquipmentInfo)
    {
        return toAjax(tEquipmentInfoService.insertTEquipmentInfo(tEquipmentInfo));
    }

    /**
     * 修改设备信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:info:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit(interval = 1000, message = "请求过于频繁！")
    @PutMapping
    public AjaxResult edit(@RequestBody TEquipmentInfo tEquipmentInfo)
    {
        return toAjax(tEquipmentInfoService.updateTEquipmentInfo(tEquipmentInfo));
    }

    /**
     * 删除设备信息
     */
    @PreAuthorize("@ss.hasPermi('equipment:info:remove')")
    @Log(title = "设备信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable String id)
    {
        return toAjax(tEquipmentInfoService.deleteTEquipmentInfoById(id));
    }

    @Log(title = "设备信息", businessType = BusinessType.IMPORT)
    @PreAuthorize("@ss.hasPermi('equipment:info:import')")
    @RepeatSubmit(interval = 1000, message = "请求过于频繁！")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception
    {
        ExcelUtil<EquipmentImportVo> util = new ExcelUtil<>(EquipmentImportVo.class);
        List<EquipmentImportVo> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = tEquipmentInfoService.importEquipment(userList, operName);
        return success(message);
    }

    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<EquipmentImportVo> util = new ExcelUtil<>(EquipmentImportVo.class);
        util.importTemplateExcel(response, "设备数据");
    }

    /**
     * 分配设备
     * @return
     */
    @Log(title = "设备信息", businessType = BusinessType.DISTRIBUTION)
    @PreAuthorize("@ss.hasPermi('equipment:info:distribution')")
    @RepeatSubmit(interval = 1000, message = "请求过于频繁！")
    @PostMapping("/distribution")
    public AjaxResult distribution(@RequestBody EquipmentDistributionReq req) {
        tEquipmentInfoService.doDistribution(req);
        return AjaxResult.success();
    }

    /**
     * 回收设备
     * @return
     */
    @Log(title = "设备信息", businessType = BusinessType.RECOVERY)
    @PreAuthorize("@ss.hasPermi('equipment:info:recovery')")
    @RepeatSubmit(interval = 1000, message = "请求过于频繁！")
    @PutMapping("/recovery/{ids}")
    public AjaxResult recovery(@PathVariable String[] ids)
    {
        return tEquipmentInfoService.doRecovery(ids);
    }

    /**
     * 修改多参数监测仪测试状态
     * @return
     */
    @PreAuthorize("@ss.hasPermi('equipment:info:edit')")
    @Log(title = "设备信息", businessType = BusinessType.UPDATE)
    @PutMapping("changeTestStatus")
    public AjaxResult changeTestStatus(@RequestBody THospitalEquipment hosEq){
        tEquipmentInfoService.changeTestStatus(hosEq);
        return AjaxResult.success();
    }
}
